<template>
  <div class="auth-page-container login-page">
    <div class="auth-box">
      <div class="auth-image-area">
        <img src="@/assets/styles/first.jpg" alt="Login Image" class="auth-image" />
      </div>
      <div class="auth-form-area">
        <el-card class="auth-card login-card">
          <div slot="header" class="card-header">
            <h2>登录</h2>
          </div>
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            label-width="80px"
            class="login-form"
          >
            <el-form-item label="用户名" prop="username">
              <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="用户名" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="loginForm.password"
                prefix-icon="el-icon-lock"
                type="password"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>
            <el-form-item label="登录身份" prop="role">
              <el-radio-group v-model="loginForm.role">
                <el-radio label="user">普通用户</el-radio>
                <el-radio label="admin">管理员</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
          <div class="button-row">
              <el-button type="primary" :loading="loading" @click="handleLogin">
                登录
              </el-button>
            </div>
          <div class="auth-links">
             <router-link to="/register" class="create-account-link">创建一个新的账户</router-link>
             <!-- 第三方登录图标位置 -->
             <div class="social-icons">
                 <!-- Icons go here -->
             </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      // 登录表单数据
      loginForm: {
        username: "",
        password: "",
        role: "user" // 默认普通用户登录
      },
      // 表单验证规则
      loginRules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      // 加载状态
      loading: false
    };
  },
  methods: {
    // 处理登录请求
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          try {
            // 调用登录action
            await this.$store.dispatch("login", {
              username: this.loginForm.username,
              password: this.loginForm.password,
              role: this.loginForm.role
            });
            // 登录成功后跳转
            const redirect = this.$route.query.redirect || "/";
            this.$router.push(redirect);
            this.$message.success("登录成功");
          } catch (error) {
            this.$message.error(error.message || "登录失败");
          } finally {
            this.loading = false;
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.auth-page-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.auth-box {
  display: flex;
  width: 800px;
  max-width: 90%;
  height: 500px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.auth-image-area {
  flex: 1.2;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
  overflow: hidden;
}

.auth-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 10px;
}

.auth-form-area {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
}

.auth-card {
    width: 100%;
    border: none;
    box-shadow: none;
    display: flex;
    flex-direction: column;
}

.card-header {
  text-align: center;
  font-size: 1.2em;
  margin: 0;
  padding: 5px 0;
  border-bottom: none;
}

.login-form {
    flex-grow: 1;
}

.auth-form-area .el-form-item {
    margin-bottom: 20px;
}

.button-row {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  margin-bottom: 20px;
  width: 100%;
}

.button-row .el-button {
    width: 100%;
}

.auth-links {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 0;
    border-top: none;
    gap: 10px;
}

.create-account-link {
    text-decoration: none;
    color: #409eff;
    font-size: 0.9em;
}

.create-account-link:hover {
    text-decoration: underline;
}

.social-icons {
}

</style>
